package com.bjpowernode.p2p.service.loan.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.p2p.dao.LoanDao;
import com.bjpowernode.p2p.dao.ReportDao;
import com.bjpowernode.p2p.entity.BLoanInfo;
import com.bjpowernode.p2p.service.loan.LoanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;



@Slf4j
//进入Spring
@Component
//暴露服务
//版本号代表，可以有多个实现【必须带上】
@Service(interfaceClass = LoanService.class, version = "1.0")
public class LoanServiceImpl implements LoanService {

    @Autowired
    ReportDao reportDao;
    @Autowired
    LoanDao loanDao;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;


    @Override
    public double avgLoanRate() {
        Double avgLoanRateReport = (Double) redisTemplate.opsForValue().get("avgLoanRateReport");
        if (!ObjectUtils.allNotNull(avgLoanRateReport)) {
            synchronized (this) {
                avgLoanRateReport = (Double) redisTemplate.opsForValue().get("avgLoanRateReport");
                if (!ObjectUtils.allNotNull(avgLoanRateReport)) {
                    avgLoanRateReport = reportDao.avgLoanRateReport();
                    log.info("数据库查询:avgLoanRateReport");
                    redisTemplate.opsForValue().set("avgLoanRateReport", avgLoanRateReport, 30, TimeUnit.SECONDS);
                }
            }
        }
        return avgLoanRateReport;
    }

    @Override
    public double sumBidMoney() {
        Double sumBidMoneyReport = (Double) redisTemplate.opsForValue().get("sumBidMoneyReport");
        if (!ObjectUtils.allNotNull(sumBidMoneyReport)) {
            synchronized (this) {
                sumBidMoneyReport = (Double) redisTemplate.opsForValue().get("sumBidMoneyReport");
                if (!ObjectUtils.allNotNull(sumBidMoneyReport)) {
                    sumBidMoneyReport = reportDao.sumBidMoneyReport();
                    log.info("数据库查询sumBidMoney");
                    redisTemplate.opsForValue().set("sumBidMoneyReport", sumBidMoneyReport, 30, TimeUnit.SECONDS);
                }
            }
        }
        return sumBidMoneyReport;
    }

    @Override
    public List<BLoanInfo> queryByproductType(HashMap<String, Object> paramMap) {
        String key = StringUtils.join(paramMap.get("productType"), paramMap.get("currentPage"), paramMap.get("pageRows"));
        List<BLoanInfo> bLoanInfos = (List<BLoanInfo>) redisTemplate.opsForValue().get(key);
        if (!ObjectUtils.allNotNull(bLoanInfos)) {
            synchronized (this) {
                bLoanInfos = (List<BLoanInfo>) redisTemplate.opsForValue().get(key);
                if (!ObjectUtils.allNotNull(bLoanInfos)) {
                    bLoanInfos = loanDao.findByproductType(paramMap);
                    log.info(StringUtils.join("数据库查询queryByproductType.....................",key));
                    redisTemplate.opsForValue().set(key, bLoanInfos, 30, TimeUnit.SECONDS);
                }
            }
        }
        return bLoanInfos;
    }
}
